package com.daxia.ogmall.web.controller.m;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.ogmall.common.NeedLogin;
import com.daxia.ogmall.dto.DecorateNoteCommentDTO;
import com.daxia.ogmall.dto.DecorateNoteDTO;
import com.daxia.ogmall.dto.UserDTO;
import com.daxia.ogmall.dto.api.DecorateNoteCommentAPIDTO;
import com.daxia.ogmall.dto.api.info.DecorateNoteCommentInfoAPIDTO;
import com.daxia.ogmall.model.DecorateNote;
import com.daxia.ogmall.model.User;
import com.daxia.ogmall.support.MPage;

@Controller
@RequestMapping(value = "/m/decorateNoteComment", produces = "application/json;charset=UTF-8")
public class MDecorateNoteCommentController extends MBaseController {
    
    @ResponseBody
    @RequestMapping("list")
    public String list(DecorateNoteCommentDTO dto, MPage page) throws Exception {
    
        List<DecorateNoteCommentDTO> decorateNoteCommentDTOs = decorateNoteCommentService.find(dto, page);

        return toApiOutput(decorateNoteCommentDTOs, DecorateNoteCommentAPIDTO.class, DecorateNoteCommentInfoAPIDTO.class, page);
    }
    
    @NeedLogin
    @ResponseBody
    @RequestMapping("save")
    public String save(DecorateNoteCommentDTO dto, String decorateNoteId) throws Exception {
    	super.assertNotBlank(decorateNoteId, "软装笔记不能为空");
    	
    	UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
    	DecorateNote decorateNote = new DecorateNote();
    	DecorateNoteDTO decorateNoteDTO = decorateNoteService.load(Long.parseLong(decorateNoteId));
    	BeanUtils.copyProperties(decorateNoteDTO, decorateNote);
    	dto.setDecorateNote(decorateNote);
    	dto.setUser(user);
    	dto.setCreateTime(new Date());
    	decorateNoteCommentService.create(dto);
    	return toJson();
    }
    
}
